package com.medistat.reports;

import java.io.IOException;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.medistat.dialog.Schedule;
import com.medistat.dialog.ScheduleCell;
import com.medistat.dialog.ScheduleSelection;
import com.medistat.model.Doctor;
import com.medistat.model.enums.Days;
import com.medistat.persistance.service.interfaces.PersistanceService;
import com.medistat.util.DaysUtils;

@Service("scheduleExcelExporter")
public class ScheduleExcelExporter implements Exporter {
	
	private PersistanceService persistanceService;
	
	@Autowired
	public void setPersistanceService(PersistanceService persistanceService) {
		this.persistanceService = persistanceService;
	}
	
	public PersistanceService getPersistanceService() {
		return persistanceService;
	}

	public void export(List<Schedule> schedules) {
		HSSFWorkbook wb = new HSSFWorkbook();
		for (Schedule schedule : schedules) {
			HSSFSheet sheet = wb.createSheet(schedule.getYear() + "_" + schedule.getWeek());
			
			HSSFRow row = sheet.createRow(0);
			HSSFCellStyle style = wb.createCellStyle();
			style.setFillBackgroundColor(HSSFColor.AQUA.index);
			style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
			style.setFillForegroundColor(HSSFColor.ORANGE.index);
			style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
			
			row.createCell(0);
			createHeader(row, null, schedule.getScheduleSelections().get(0).getScheduleCells());
			
			//other rows - doctors
			int i = 1;
			for (ScheduleSelection scheduleSelection : schedule.getScheduleSelections()) {
				int j = 1;
				row = sheet.createRow(i);
				row.createCell(0).setCellValue(scheduleSelection.getTypeOfWorkDescription());
				for (ScheduleCell scheduleCell : scheduleSelection.getScheduleCells()) {
					//if doctor has been manually changed after schedule generate on gui.
					if((scheduleCell.getDoctor()==null && scheduleCell.getDoctorId()!= null) || (scheduleCell.getDoctor()!=null && !scheduleCell.getDoctor().getId().equals(scheduleCell.getDoctorId()))) {
						Doctor doctor = null;
						try {
							doctor = getPersistanceService().getDoctor(scheduleCell.getDoctorId());
						} catch (Exception e) {
							e.printStackTrace();
						}
						scheduleCell.setDoctor(doctor);
					}
					row.createCell(j++).setCellValue(scheduleCell.getDoctorDescription());
				}
				i++;
			}
		}
		String fileName = "Urnik " + schedules.get(0).getYear()	+ ".xls";
		writeWorkBookToOuputStream(wb, fileName);
	}

	private void writeWorkBookToOuputStream(HSSFWorkbook workBook, String fileName) {
		HttpServletResponse res = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
		res.setContentType("application/vnd.ms-excel");
		res.setHeader("Content-disposition", "attachment; filename="+ fileName);
		try {
			ServletOutputStream out = res.getOutputStream();

			workBook.write(out);
			out.flush();
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		FacesContext faces = FacesContext.getCurrentInstance();
		faces.responseComplete();
	}

	private void createHeader(HSSFRow row, HSSFCellStyle style, List<ScheduleCell> scheduleCells) {
		int i = 1;
		Days[] values = Days.values();
		for(ScheduleCell scheduleCell : scheduleCells) {
			HSSFCell cell = row.createCell(i);
			Days day = values[i-1];
			cell.setCellValue(DaysUtils.getDaysDescription(day) + " " + scheduleCell.getDateDescription());
			i++;
		}
	}
}
